$(function() {

    /*导航栏全局变量*/
    var ajaxPage=2;
    var isMaxPage=false;
    var ajaxPhp=base_url+"index.php/wap/ajax/prize/";
    var dataDom="ul-prize";
    var url_prefix = base_url+'index.php/wap/index/togame/';


    var ajaxPages=Array();
    ajaxPages['ul-prize']=3;
    ajaxPages['ul-hots']=3;
    ajaxPages['ul-recommend']=3;
    ajaxPages['ul-libao']=3;
    ajaxPages['ul-dingzhi']=3;

    var isMaxPages=Array();
    isMaxPages['ul-prize']=0;
    isMaxPages['ul-hots']=0;
    isMaxPages['ul-recommend']=1;
    isMaxPages['ul-libao']=0;
    isMaxPages['ul-dingzhi']=0;

    var ajaxPhps=Array();
    ajaxPhps['ul-prize']=base_url+"index.php/wap/ajax/prize/";
    ajaxPhps['ul-hots']=base_url+"index.php/wap/ajax/hot/";
    ajaxPhps['ul-recommend']=base_url+"index.php/wap/ajax/recommend/";
    ajaxPhps['ul-libao']=base_url+"index.php/wap/ajax/hot/";
    ajaxPhps['ul-dingzhi']=base_url+"index.php/wap/ajax/hot/";


    /*导航选项卡*/
    $('.navs a').click(function(e) {
        e.preventDefault(); //阻止a链接的跳转行为

        $('.navs a').removeClass('active');
        $(this).addClass('active');
        $(this).tab('show'); //显示当前选中的链接及关联的content

        //判断是否有奖选项卡
        // if($(this).hasClass('city')){//显示城市列表
        //     $(".citys").removeClass('hide');
        // }else{
        //     $(".citys").addClass('hide');
        // }

        $(".citys").addClass('hide');//隐藏城市切换下拉列表

        //获取当前导航栏参数
        dataDom=$(this).attr("data-add");
        ajaxPage=ajaxPages[dataDom];
        isMaxPage=isMaxPages[dataDom];
        ajaxPhp=ajaxPhps[dataDom];
    })

    // 搜索，结果显示在热门游戏页面
    $('.icon-search').click(function(e) {
        e.preventDefault(); //阻止a链接的跳转行为

        dataDom = 'ul-hots';
        ajaxPhp=ajaxPhps[dataDom];
        var url  = ajaxPhp + "?title="+$('#search').val();
        $('.'+dataDom).html('');
        ajaxData(1,url,dataDom);

        $('.navs a').removeClass('active');
        $('.navs a#'+dataDom).addClass('active')
                             .tab('show');
    });

    /*推荐选项卡*/
    $('.tab-recomend a').click(function(e) {
        e.preventDefault(); //阻止a链接的跳转行为
        $('.tab-recomend a').removeClass('active');
        $(this).addClass('active');
        $(this).tab('show'); //显示当前选中的链接及关联的content

        var remmendType=$(this).attr("data-type");
        ajaxPages['ul-recommend']=2;
        ajaxPhps['ul-recommend']=base_url+"index.php/wap/ajax/recommend/"+"?remmendType="+remmendType;
    })

    $(".owl-carousel").owlCarousel({
        autoPlay : 5000
    });


    /*滚动到底刷新*/
    var scrolltop=0;
    var scrollDirection=0;
    var g_flag = true;

    //var ajaxPage=2;
    //var isMaxPage=false;
    $(window).scroll(function(event) {
        /* Act on the event */
        // console.log('页面高度：'+$(document).height());
        // console.log('已滚上去高度：'+$(this).scrollTop());
        // console.log('浏览器窗口高度：'+$(this).height());
        //var limit = $(document).height() - $(this).scrollTop() - document.getElementsByTagName('body')[0].offsetHeight;

        var limit = $(document).height() - $(this).scrollTop() - $(this).height();

        if($(this).scrollTop()>scrolltop){
            scrollDirection=1;
        }else{
            scrollDirection=0;
        }
        scrolltop=$(this).scrollTop();
        if (limit < 100 && g_flag && scrollDirection==1 && isMaxPage==false) {
            g_flag=false;
            ajaxData(ajaxPage,ajaxPhp,dataDom);
            g_flag = true;

        }

        //控制置顶按钮显示
        if($(window).scrollTop()>200){
            $(".fixed-group").show();
        }else if($(window).scrollTop()<200){
            $(".fixed-group").hide();
        }

    });

    /**
     * 置顶按钮操作
     */
    $(".fixed-group .top-btn").click(function(){
        $(window).scrollTop(0);
    });

    /*请求加载新内容*/
    function ajaxData(page,ajaxPhp,dataDom){

        $.ajax({
            url: ajaxPhp,
            type: 'GET',
            dataType: 'json',
            data: {ajaxPage: page},
            async: false
        })
            .done(function(data) {
                if(data['msg']=="1" && data['length']>0){
                    ajaxPage++;
                    addData(data,dataDom);//添加数学
                }else{
                    //console.log(data['msg'])
                    //alert('no more');
                    isMaxPage=true;
                }
            })
            .fail(function() {
                //console.log("error");
            })
            .always(function() {
                //console.log("complete");
            });
    }

    //添加数据
    function addData(data,dataDom){
        switch (dataDom)
        {
            case "ul-prize":
                addDataPrize(data,dataDom);
                break;
            case "ul-hots":
                addDataHot(data,dataDom);
                break;
            case "ul-recommend":
                addDataRecommend(data,dataDom);
                break;
            case "ul-libao":
                addDataDingzhi(data,dataDom)
                break;
        }

    }

    //添加“热门”栏目数据
    function addDataHot(data,dataDom){
        var length=data['length'];
        var arrs=data['arrs'];


        for(var i=0;i<length;i++){
            var li=$(".ul-hots-pre>li:first-child").clone();
            li.find(".thumb").attr("src",arrs[i]['picture']);
            li.find(".hd").text(arrs[i]['title']);
            li.find(".popularity").text(arrs[i]['click']);
            var str = "javascript:click_ajax("+arrs[i]['id']+',"'+handle_url(arrs[i]['id'],'hot')+'")';
            li.find(".btn-play").attr("href",str);
            li.find(".desc").text(arrs[i]['description']);

            $(".ul-hots").append(li);

        }
    }

    //添加“有奖”栏目数据
    function addDataPrize(data,dataDom){
        var length=data['length'];
        var arrs=data['arrs'];

        for(var i=0;i<length;i++){
            var li=$(".ul-prize-pre>li:first-child").clone();
            li.find(".thumb").attr("src",arrs[i]['picture']);
            li.find(".hd").text(arrs[i]['title']);
            li.find(".popularity").text(arrs[i]['click']);
            var str = "javascript:click_ajax("+arrs[i]['id']+',"'+handle_url(arrs[i]['id'],'new')+'")';
            li.find(".btn-play").attr("href",str);
            li.find(".desc").text(arrs[i]['description']);
            li.find(".client_title").text(arrs[i]['client_title']);
            li.find(".prize").text(arrs[i]['prize']);
            li.find(".endtime").text(arrs[i]['endtime']);

            $(".ul-prize").append(li);
        }
    }

    /**
     * 处理页面游戏链接地址
     * @param game_id 游戏id
     * @param channel 游戏渠道
     * @returns {string} 游戏在页面展示出来的访问地址
     */
    function handle_url(game_id,channel) {
        if(channel==''){
            channel='list';
        }

        return url_prefix+game_id+'?channel='+channel;
    }

    //----------------------历史遗留，归档处理--------------------------//
    //有奖栏目 改变城市,暂时不用
    $(".sel-citys").change(function(){
        var cityid = $('#cityid').val();
        ajaxPhps['ul-prize']= base_url+"index.php/wap/ajax/prize/"+'?cityid='+cityid;
        $('.'+dataDom).html('');
        ajaxPages['ul-prize']=2;
        ajaxData(1,ajaxPhps['ul-prize'],"ul-prize");
    })

    //选择城市，暂时不用
    $('.dropdown-menu a').bind('click',function(e){

        $('.cityid').html($(this).html());
        var cityid=$(this).attr("data-type");
        ajaxPhps['ul-prize']= base_url+"index.php/wap/ajax/prize/"+'?cityid='+cityid;
        $('.'+dataDom).html('');
        ajaxPages['ul-prize']=2;
        ajaxData(1,ajaxPhps['ul-prize'],"ul-prize");
    });

    //添加“推荐”栏目数据，暂时不用
    function addDataRecommend(data,dataDom){
        var length=data['length'];
        var arrs=data['arrs'];

        for(var i=0;i<length;i++){
            var li=$(".ul-recommend-pre>li:first-child").clone();
            li.find(".thumb").attr("src",arrs[i]['picture']);
            li.find(".hd").text(arrs[i]['title']);
            li.find(".popularity").text(arrs[i]['click']);
            li.find(".btn-play").attr("href",arrs[i]['url']);

            $(".ul-recommend").append(li);
        }
    }

    //添加“礼包”栏目数据，暂时不用
    function addDataDingzhi(data,dataDom){
        var length=data['length'];
        var arrs=data['arrs'];

        for(var i=0;i<length;i++){
            var li=$(".ul-libao-pre>li:first-child").clone();
            li.find(".thumb").attr("src",arrs[i]['picture']);
            li.find(".hd").text(arrs[i]['title']);
            li.find(".popularity").text(arrs[i]['click']);
            li.find(".btn-play").attr("href",arrs[i]['url']);

            $(".ul-libao").append(li);
        }
    }

});



